/*  Primer v3 - yet another prime counter.
 *
 *  This one will take an argument and count/print
 *  that many prime numbers. Streamlined from v2.
 *  Logging file will be created in same dir as Primer3.
 *
 *  Syntax: "primer3 *number_of_primes_to_find* >> *name_of_log_file*"
 *
 *  ~Xevious 2-10-09
 */
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
bool prime(unsigned long number);
unsigned long xprime; // xprime stores prime # to test.
int x = 0; // x stores # of primes found.
long arg;
int main(int argc, char* argv[])
{
    system("title Generating some prime numbers...");
    for (int i = 1; i < argc; ++i)
    {
        arg += atoi(argv[i]);
    }
    //cout << "int arg = (" << arg << ")\n";
    for (xprime = 2; x < arg; (xprime == 2) ? xprime = 3 : xprime += 2)
    {
        if (prime(xprime))
        {
            if (x == (arg - 1))
            {
                cout << "\n\nPrime # " << arg << ": ";
            } 
            cout << xprime << " ";
            x += 1;
        }
    }
    return EXIT_SUCCESS;
}
bool prime(unsigned long number) // this function will test if a number is a prime number 
{							     // or not. Boolean (true or false).
    if (number == 2) // added this to align my results with online tables.
    {
        return true; 
    }
    else if (number % 2 == 0) // if number div 2, then it's even.
    {
        return false;
    }
    else
    {
        int highfactor = int(sqrt(number)+1); // highest number to check against.
        int divisor = 3; // start dividing by 3.
        while (divisor <= highfactor) // check for division.
        {
              if (number % divisor == 0)
              {
                         return false;
              }
              divisor += 2;
        }
        return true; // well if it got here then it must be a prime.
   }
}
